package lz.controls.scroll
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	import lz.controls.LButton;
	
	public class SimpleArrowScroll extends EventDispatcher
	{
		/**
		 * 皮肤 上或左
		 */		
		public var _upMc		 :MovieClip;
		/**
		 * 皮肤 下或右
		 */		
		public var _downMc		 :MovieClip;
		
		public var upBtn :LButton;
		public var downBtn :LButton;
		
		private var _currentPlace :Number = 0;
		/**
		 *每一次点击增量 
		 */		
		private var _stepDistance :Number = 55;
		/**
		 *连续增量 
		 */		
		private var _continueDistance :Number = 11;
		
		public function SimpleArrowScroll()
		{
			
		}
		
		public function set stepDistance(value :Number):void
		{
			_stepDistance = value;
		}
		
		public function get stepDistance():Number
		{
			return _stepDistance;
		}
		
		public function set continueDistance(value :Number):void
		{
			_continueDistance = value;
		}
		
		public function get continueDistance():Number
		{
			return _continueDistance;
		}
		
		public function initView():void
		{
			upBtn = new LButton(_upMc);
			downBtn = new LButton(_downMc);
			upBtn.enable = true;
			upBtn.buttonMode = true;
			downBtn.enable = true;
			downBtn.buttonMode = true;
			addEvtListener();
		}
		
		public function addEvtListener():void
		{
			upBtn.addEventListener(MouseEvent.CLICK,onUpHandler);
			upBtn.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
			downBtn.addEventListener(MouseEvent.CLICK,onDownHandler);
			downBtn.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
		}
		private function onUpHandler(e :MouseEvent):void
		{
			currentPlace = _stepDistance;
		}
		
		private function onDownHandler(e :MouseEvent):void
		{
			currentPlace = -_stepDistance;
		}
		private var _isDown:Boolean = false;
		private var _isUp:Boolean = false;
		private var times :Number ;
		private function onMouseDownHandler(e :MouseEvent):void
		{
			if (e.currentTarget == upBtn)
			{
				
				_isUp = true;
				_isDown = false;
			} else 
			{
				if (e.currentTarget == downBtn)
				{
					
					_isUp = false;
					_isDown = true;
				}
			}
			times = getTimer();
			//_upMc.addEventListener(Event.ENTER_FRAME, scrollHandler);
			//_upMc.stage.addEventListener(MouseEvent.MOUSE_UP, stopScroll);
		}
		private function scrollHandler(e:Event):void
		{
			if(getTimer() - times < 500)
			{
				return;
			}
			if (_isUp)
			{
				currentPlace = _continueDistance;
			}
			if (_isDown)
			{
				currentPlace = -_continueDistance;
			}
		}
		private function stopScroll(e:MouseEvent):void
		{
			_upMc.removeEventListener(Event.ENTER_FRAME, scrollHandler);
			_upMc.stage.removeEventListener(MouseEvent.MOUSE_UP, stopScroll);
		}
		
		public function set currentPlace(value :Number):void
		{
			_currentPlace = value;
			///
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function get currentPlace():Number
		{
			return _currentPlace;
		}
		
		public function setVisible(value :Boolean):void
		{
			_upMc.visible = value;
			_downMc.visible = value;
		}
	}
}